package com.screen.screendisplay.service.impl.coal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.screen.screendisplay.entity.coal.CoalOpermonEquipUse;
import com.screen.screendisplay.mapper.coal.CoalOpermonEquipUseMapper;
import com.screen.screendisplay.service.coal.CoalOpermonEquipUseService;
import com.screen.screendisplay.vo.coal.CoalEquipmentUsageVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 * 运行检测设备动用信息表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class CoalOpermonEquipUseServiceImpl extends ServiceImpl<CoalOpermonEquipUseMapper, CoalOpermonEquipUse> implements CoalOpermonEquipUseService {

    @Override
    public List<CoalEquipmentUsageVO> getEquipmentUsage(Integer year) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        
        // 计算6个月前的日期
        LocalDate sixMonthsAgo = currentDate.minusMonths(5);
        
        // 查询最近6个月的数据
        LambdaQueryWrapper<CoalOpermonEquipUse> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(CoalOpermonEquipUse::getYear, sixMonthsAgo.getYear())
               .orderByDesc(CoalOpermonEquipUse::getYear)
               .orderByDesc(CoalOpermonEquipUse::getMonth);
        
        List<CoalOpermonEquipUse> equipmentList = this.list(wrapper);
        
        // 按年月分组统计
        Map<String, MonthlyEquipmentStats> monthlyStatsMap = new HashMap<>();
        
        for (CoalOpermonEquipUse equipment : equipmentList) {
            String yearMonth = equipment.getYear() + "-" + equipment.getMonth();
            MonthlyEquipmentStats stats = monthlyStatsMap.computeIfAbsent(
                yearMonth, 
                k -> new MonthlyEquipmentStats(equipment.getYear(), equipment.getMonth())
            );
            
            // 按设备类型统计
            int type = equipment.getEquipmentType() != null ? equipment.getEquipmentType() : 0;
            int count = equipment.getEquipmentCount() != null ? equipment.getEquipmentCount() : 0;
            int isUsing = equipment.getIsUsing() != null ? equipment.getIsUsing() : 0;
            
            // 累计设备总数
            stats.totalCount += count;
            
            // 按设备类型统计
            if (type == 0) {
                // 运输车
                stats.truckTotal += count;
                if (isUsing == 1) {
                    stats.truckUsing += count;
                }
            } else if (type == 1) {
                // 附属设备
                stats.auxiliaryTotal += count;
                if (isUsing == 1) {
                    stats.auxiliaryUsing += count;
                }
            } else if (type == 2) {
                // 电铲
                stats.shovelTotal += count;
                if (isUsing == 1) {
                    stats.shovelUsing += count;
                }
            }
        }
        
        // 转换为VO列表
        List<CoalEquipmentUsageVO> resultList = new ArrayList<>();
        
        // 生成最近6个月的数据
        for (int i = 0; i < 6; i++) {
            LocalDate monthDate = currentDate.minusMonths(i);
            String yearMonth = monthDate.getYear() + "-" + monthDate.getMonthValue();
            
            CoalEquipmentUsageVO vo = new CoalEquipmentUsageVO();
            vo.setYear(monthDate.getYear());
            vo.setMonth(monthDate.getMonthValue());
            
            MonthlyEquipmentStats stats = monthlyStatsMap.get(yearMonth);
            if (stats != null) {
                vo.setEquipmentCount(stats.totalCount);
                
                // 计算运输车出动率
                if (stats.truckTotal > 0) {
                    BigDecimal truckRate = new BigDecimal(stats.truckUsing)
                            .divide(new BigDecimal(stats.truckTotal), 4, RoundingMode.HALF_UP)
                            .multiply(new BigDecimal("100"))
                            .setScale(2, RoundingMode.HALF_UP);
                    vo.setTruckUsageRate(truckRate);
                } else {
                    vo.setTruckUsageRate(BigDecimal.ZERO);
                }
                
                // 计算附属设备出动率
                if (stats.auxiliaryTotal > 0) {
                    BigDecimal auxiliaryRate = new BigDecimal(stats.auxiliaryUsing)
                            .divide(new BigDecimal(stats.auxiliaryTotal), 4, RoundingMode.HALF_UP)
                            .multiply(new BigDecimal("100"))
                            .setScale(2, RoundingMode.HALF_UP);
                    vo.setAuxiliaryUsageRate(auxiliaryRate);
                } else {
                    vo.setAuxiliaryUsageRate(BigDecimal.ZERO);
                }
                
                // 计算电铲出动率
                if (stats.shovelTotal > 0) {
                    BigDecimal shovelRate = new BigDecimal(stats.shovelUsing)
                            .divide(new BigDecimal(stats.shovelTotal), 4, RoundingMode.HALF_UP)
                            .multiply(new BigDecimal("100"))
                            .setScale(2, RoundingMode.HALF_UP);
                    vo.setShovelUsageRate(shovelRate);
                } else {
                    vo.setShovelUsageRate(BigDecimal.ZERO);
                }
            } else {
                vo.setEquipmentCount(0);
                vo.setTruckUsageRate(BigDecimal.ZERO);
                vo.setAuxiliaryUsageRate(BigDecimal.ZERO);
                vo.setShovelUsageRate(BigDecimal.ZERO);
            }
            
            resultList.add(vo);
        }
        
        return resultList;
    }
    
    /**
     * 月度设备统计数据内部类
     */
    private static class MonthlyEquipmentStats {
        int totalCount = 0;
        int truckTotal = 0;
        int truckUsing = 0;
        int auxiliaryTotal = 0;
        int auxiliaryUsing = 0;
        int shovelTotal = 0;
        int shovelUsing = 0;
        
        public MonthlyEquipmentStats(int year, int month) {
            // 构造函数接收参数但不需要存储
        }
    }
}
